00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _base_factory_h_
00019 #define _base_factory_h_
00020
00021 #include <vector>
00022 #include "boost/smart_ptr/shared_ptr.hpp"
00023 #include "gridpack/timer/coarse_timer.hpp"
00024 #include "gridpack/network/base_network.hpp"
00025 #include "gridpack/component/base_component.hpp"
00026
00027
00028
00029
00030 namespace gridpack{
00031 namespace factory{
00032
00033 template <class _network>
00034 class BaseFactory {
00035 public:
00036 typedef _network NetworkType;
00037 typedef boost::shared_ptr<NetworkType> NetworkPtr;
00038
00039
00040
00041
00042
00043 BaseFactory(NetworkPtr network)
00044 : p_network(network)
00045 {
00046 p_profile = false;
00047 p_numBuses = p_network->numBuses();
00048 p_numBranches = p_network->numBranches();
00049 p_buses = new gridpack::component::BaseBusComponent*[p_numBuses];
00050 p_branches = new gridpack::component::BaseBranchComponent*[p_numBranches];
00051 int i;
00052 for (i=0; i<p_numBuses; i++) {
00053 p_buses[i] = p_network->getBus(i).get();
00054 }
00055 for (i=0; i<p_numBranches; i++) {
00056 p_branches[i] = p_network->getBranch(i).get();
00057 }
00058 }
00059
00060
00061
00062
00063 virtual ~BaseFactory(void)
00064 {
00065 delete [] p_buses;
00066 delete [] p_branches;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 virtual void setComponents(void)
00076 {
00077 gridpack::utility::CoarseTimer *timer =
00078 gridpack::utility::CoarseTimer::instance();
00079 timer->configTimer(p_profile);
00080 int t_setc = timer->createCategory("Factory:setComponents");
00081 timer->start(t_setc);
00082 int i, j;
00083 int idx1, idx2;
00084
00085
00086 int numActiveBranch = 0;
00087 for (i=0; i<p_numBranches; i++) {
00088 int branch_idx, bus1_idx, bus2_idx;
00089 p_network->getBranchEndpoints(i, &idx1, &idx2);
00090 p_network->getBranch(i)->setBus1(p_network->getBus(idx1));
00091 p_network->getBranch(i)->setBus2(p_network->getBus(idx2));
00092 branch_idx = p_network->getGlobalBranchIndex(i);
00093 bus1_idx = p_network->getGlobalBusIndex(idx1);
00094 bus2_idx = p_network->getGlobalBusIndex(idx2);
00095 p_network->getBranch(i)->setMatVecIndices(bus1_idx, bus2_idx);
00096 if (p_network->getActiveBranch(i)) numActiveBranch++;
00097 }
00098
00099
00100 int numActiveBus = 0;
00101 for (i=0; i<p_numBuses; i++) {
00102 p_network->getBus(i)->clearBuses();
00103 std::vector<int> nghbrBus = p_network->getConnectedBuses(i);
00104 for (j=0; j<nghbrBus.size(); j++) {
00105 p_network->getBus(i)->addBus(p_network->getBus(nghbrBus[j]));
00106 }
00107 p_network->getBus(i)->clearBranches();
00108 std::vector<int> nghbrBranch = p_network->getConnectedBranches(i);
00109 for (j=0; j<nghbrBranch.size(); j++) {
00110 p_network->getBus(i)->addBranch(p_network->getBranch(nghbrBranch[j]));
00111 }
00112 int bus_idx;
00113 bus_idx = p_network->getGlobalBusIndex(i);
00114 p_network->getBus(i)->setMatVecIndex(bus_idx);
00115 if (p_network->getActiveBus(i)) numActiveBus++;
00116 }
00117
00118
00119 int idx = p_network->getReferenceBus();
00120 if (idx != -1) {
00121 p_network->getBus(idx)->setReferenceBus(true);
00122 }
00123
00124
00125 for (i=0; i<p_numBuses; i++) {
00126 p_network->getBus(i)->setOriginalIndex(p_network->getOriginalBusIndex(i));
00127 p_network->getBus(i)->setGlobalIndex(p_network->getGlobalBusIndex(i));
00128 }
00129 for (i=0; i<p_numBranches; i++) {
00130 gridpack::component::BaseBusComponent *bus1 =
00131 dynamic_cast<gridpack::component::BaseBusComponent*>
00132 (p_network->getBranch(i)->getBus1().get());
00133 gridpack::component::BaseBusComponent *bus2 =
00134 dynamic_cast<gridpack::component::BaseBusComponent*>
00135 (p_network->getBranch(i)->getBus2().get());
00136 p_network->getBranch(i)->setGlobalIndex(
00137 p_network->getGlobalBranchIndex(i));
00138 p_network->getBranch(i)->setBus1OriginalIndex(
00139 bus1->getOriginalIndex());
00140 p_network->getBranch(i)->setBus2OriginalIndex(
00141 bus2->getOriginalIndex());
00142 p_network->getBranch(i)->setBus1GlobalIndex(
00143 bus1->getGlobalIndex());
00144 p_network->getBranch(i)->setBus2GlobalIndex(
00145 bus2->getGlobalIndex());
00146 }
00147
00148
00149
00150
00151
00152
00153 int grp = p_network->communicator().getGroup();
00154 int nprocs = GA_Pgroup_nnodes(grp);
00155 int me = GA_Pgroup_nodeid(grp);
00156 int *activeBus = new int[nprocs];
00157 int *activeBranch = new int[nprocs];
00158
00159 for (i=0; i<nprocs; i++) {
00160 activeBus[i] = 0;
00161 activeBranch[i] = 0;
00162 }
00163
00164 activeBus[me] = numActiveBus;
00165 activeBranch[me] = numActiveBranch;
00166 char cplus[2];
00167 strcpy(cplus,"+");
00168 GA_Pgroup_igop(grp,activeBus,nprocs,cplus);
00169 GA_Pgroup_igop(grp,activeBranch,nprocs,cplus);
00170
00171
00172
00173 int one = 1;
00174 int ntot = 0;
00175 int offset = 0;
00176 for (i=0; i<nprocs; i++) {
00177 ntot += activeBus[i];
00178 if (i<me) offset += activeBus[i];
00179 }
00180 int g_bus = GA_Create_handle();
00181 GA_Set_data(g_bus, one, &ntot, C_INT);
00182 GA_Set_pgroup(g_bus, grp);
00183 if (!GA_Allocate(g_bus)) {
00184 char buf[256];
00185 sprintf(buf,"BaseFactory::setComponents: Unable to allocate distributed array for indices");
00186 printf("%s",buf);
00187 throw gridpack::Exception(buf);
00188 }
00189 GA_Zero(g_bus);
00190
00191
00192 int *ibus_val = new int[numActiveBus];
00193 int **ibus_idx = new int*[numActiveBus];
00194 int icnt = 0;
00195 for (i=0; i<p_numBuses; i++) {
00196 if (p_network->getActiveBus(i)) {
00197 ibus_idx[icnt] = new int;
00198 *(ibus_idx[icnt]) = p_network->getGlobalBusIndex(i);
00199 ibus_val[icnt] = offset+icnt;
00200 icnt++;
00201 }
00202 }
00203 NGA_Scatter(g_bus,ibus_val,ibus_idx,numActiveBus);
00204 GA_Pgroup_sync(grp);
00205 for (i=0; i<numActiveBus; i++) {
00206 delete ibus_idx[i];
00207 }
00208 delete [] ibus_idx;
00209 delete [] ibus_val;
00210
00211
00212 ibus_val = new int[p_numBuses];
00213 ibus_idx = new int*[p_numBuses];
00214 for (i=0; i<p_numBuses; i++) {
00215 ibus_idx[i] = new int;
00216 *(ibus_idx[i]) = p_network->getGlobalBusIndex(i);
00217 }
00218 NGA_Gather(g_bus,ibus_val,ibus_idx,p_numBuses);
00219
00220 for (i=0; i<p_numBuses; i++) {
00221 p_network->getBus(i)->setMatVecIndex(ibus_val[i]);
00222 delete ibus_idx[i];
00223 }
00224 delete [] ibus_idx;
00225 delete [] ibus_val;
00226 delete [] activeBus;
00227 delete [] activeBranch;
00228 GA_Destroy(g_bus);
00229
00230
00231 for (i=0; i<p_numBranches; i++) {
00232 p_network->getBranch(i)->getBus1()->getMatVecIndex(&idx1);
00233 p_network->getBranch(i)->getBus2()->getMatVecIndex(&idx2);
00234 p_network->getBranch(i)->setMatVecIndices(idx1,idx2);
00235 }
00236
00237
00238 p_network->setMap();
00239 timer->stop(t_setc);
00240 timer->configTimer(true);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 virtual void load(void)
00250 {
00251 gridpack::utility::CoarseTimer *timer =
00252 gridpack::utility::CoarseTimer::instance();
00253 timer->configTimer(p_profile);
00254 int t_load = timer->createCategory("Factory:load");
00255 timer->start(t_load);
00256 int t_nbus = timer->createCategory("Factory:load:nbus");
00257 timer->start(t_nbus);
00258 timer->stop(t_nbus);
00259 int i;
00260 int rank = p_network->communicator().rank();
00261
00262
00263 int t_load1 = timer->createCategory("Factory:load:bus");
00264 timer->start(t_load1);
00265 for (i=0; i<p_numBuses; i++) {
00266 p_network->getBus(i)->setRank(rank);
00267 p_network->getBus(i)->load(p_network->getBusData(i));
00268 if (p_network->getBus(i)->getReferenceBus())
00269 p_network->setReferenceBus(i);
00270 }
00271 timer->stop(t_load1);
00272
00273
00274 int t_load2 = timer->createCategory("Factory:load:branch");
00275 timer->start(t_load2);
00276 for (i=0; i<p_numBranches; i++) {
00277 p_network->getBranch(i)->setRank(rank);
00278 p_network->getBranch(i)->load(p_network->getBranchData(i));
00279 }
00280 timer->stop(t_load2);
00281 timer->stop(t_load);
00282 timer->configTimer(true);
00283 }
00284
00285
00286
00287
00288
00289
00290
00291 virtual void setExchange(bool flag=true)
00292 {
00293 gridpack::utility::CoarseTimer *timer =
00294 gridpack::utility::CoarseTimer::instance();
00295 timer->configTimer(p_profile);
00296 int t_setx = timer->createCategory("Factory:setExchange");
00297 timer->start(t_setx);
00298 int busXCSize, branchXCSize;
00299 int nbus, nbranch;
00300
00301 nbus = p_numBuses;
00302 nbranch = p_numBranches;
00303
00304
00305
00306
00307 if (nbus > 0) {
00308 busXCSize = p_network->getBus(0)->getXCBufSize();
00309 } else {
00310 busXCSize = 0;
00311 }
00312 if (flag) {
00313 p_network->allocXCBus(busXCSize);
00314 } else {
00315 p_network->allocXCBusPointers(busXCSize);
00316 }
00317
00318 if (nbranch > 0){
00319 branchXCSize = p_network->getBranch(0)->getXCBufSize();
00320 } else {
00321 branchXCSize = 0;
00322 }
00323 if (flag) {
00324 p_network->allocXCBranch(branchXCSize);
00325 } else {
00326 p_network->allocXCBranchPointers(branchXCSize);
00327 }
00328
00329 int i;
00330 if (flag) {
00331
00332
00333 if (busXCSize > 0) {
00334 for (i=0; i<nbus; i++) {
00335 p_network->getBus(i)->setXCBuf(p_network->getXCBusBuffer(i));
00336 }
00337 }
00338 if (branchXCSize > 0) {
00339 for (i=0; i<nbranch; i++) {
00340 p_network->getBranch(i)->setXCBuf(p_network->getXCBranchBuffer(i));
00341 }
00342 }
00343 } else {
00344
00345
00346 void *ptr;
00347 if (busXCSize > 0) {
00348 for (i=0; i<nbus; i++) {
00349 p_network->getBus(i)->getXCBuf(&ptr);
00350 p_network->setXCBusBuffer(i,ptr);
00351 }
00352 }
00353 if (branchXCSize > 0) {
00354 for (i=0; i<nbranch; i++) {
00355 p_network->getBranch(i)->getXCBuf(&ptr);
00356 p_network->setXCBranchBuffer(i,ptr);
00357 }
00358 }
00359 }
00360 timer->stop(t_setx);
00361 timer->configTimer(true);
00362 }
00363
00364
00365
00366
00367
00368 virtual void setMode(int mode)
00369 {
00370 int i;
00371 for (i=0; i<p_numBuses; i++) {
00372 p_buses[i]->setMode(mode);
00373 }
00374 for (i=0; i<p_numBranches; i++) {
00375 p_branches[i]->setMode(mode);
00376 }
00377 }
00378
00379
00380
00381
00382
00383 virtual void setBusMode(int mode)
00384 {
00385 int i;
00386 for (i=0; i<p_numBuses; i++) {
00387 p_buses[i]->setMode(mode);
00388 }
00389 }
00390
00391
00392
00393
00394
00395 virtual void setBranchMode(int mode)
00396 {
00397 int i;
00398 for (i=0; i<p_numBranches; i++) {
00399 p_branches[i]->setMode(mode);
00400 }
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 bool checkTrue(bool flag) {
00410 int iok;
00411 if (flag) {
00412 iok = 1;
00413 } else {
00414 iok = 0;
00415 }
00416 int grp = p_network->communicator().getGroup();
00417 int nprocs = GA_Pgroup_nnodes(grp);
00418 char cplus[2];
00419 strcpy(cplus,"+");
00420 GA_Pgroup_igop(grp,&iok,1,cplus);
00421 if (iok == nprocs) {
00422 return true;
00423 } else {
00424 return false;
00425 }
00426 }
00427
00428
00429
00430
00431
00432
00433
00434 bool checkTrueSomewhere(bool flag) {
00435 int iok;
00436 if (flag) {
00437 iok = 1;
00438 } else {
00439 iok = 0;
00440 }
00441 int grp = p_network->communicator().getGroup();
00442 int nprocs = GA_Pgroup_nnodes(grp);
00443 char cplus[2];
00444 strcpy(cplus,"+");
00445 GA_Pgroup_igop(grp,&iok,1,cplus);
00446 if (iok > 0) {
00447 return true;
00448 } else {
00449 return false;
00450 }
00451 }
00452
00453
00454
00455
00456
00457
00458 void saveData(void)
00459 {
00460 int i;
00461
00462 for (i=0; i<p_numBuses; i++) {
00463 p_buses[i]->saveData(p_network->getBusData(i));
00464 }
00465
00466 for (i=0; i<p_numBranches; i++) {
00467 p_branches[i]->saveData(p_network->getBranchData(i));
00468 }
00469 }
00470
00471
00472
00473
00474
00475
00476 void getBusPointers(gridpack::component::BaseBusComponent ***buses,
00477 int *nbus)
00478 {
00479 *buses = p_buses;
00480 *nbus = p_numBuses;
00481 }
00482
00483
00484
00485
00486
00487
00488 void getBranchPointers(gridpack::component::BaseBranchComponent ***branches,
00489 int *nbranch)
00490 {
00491 *branches = p_branches;
00492 *nbranch = p_numBranches;
00493 }
00494
00495
00496
00497
00498
00499
00500
00501 void dumpData(void)
00502 {
00503 int i, iproc;
00504
00505 int nprocs = p_network->communicator().size();
00506 int me = p_network->communicator().rank();
00507 for (iproc = 0; iproc<nprocs; iproc++) {
00508 if (iproc == me) {
00509 for (i=0; i<p_numBuses; i++) {
00510 printf("p[%d] Printing data for bus %d\n",me,i);
00511 p_network->getBusData(i)->dump();
00512 }
00513 }
00514 GA_Sync();
00515 }
00516 for (iproc = 0; iproc<nprocs; iproc++) {
00517 if (iproc == me) {
00518 for (i=0; i<p_numBranches; i++) {
00519 printf("p[%d] Printing data for branch %d\n",me,i);
00520 p_network->getBranchData(i)->dump();
00521 }
00522 }
00523 GA_Sync();
00524 }
00525 }
00526
00527 protected:
00528
00529 NetworkPtr p_network;
00530
00531 bool p_profile;
00532
00533 int p_numBuses;
00534
00535 int p_numBranches;
00536
00537 gridpack::component::BaseBusComponent **p_buses;
00538
00539 gridpack::component::BaseBranchComponent **p_branches;
00540 };
00541
00542 }
00543 }
00544 #endif